//
//  WaterFlowLayout.m
//  瀑布流布局
//
//  Created by 红蜀黍 on 15/5/6.
//  Copyright (c) 2015年 红蜀黍. All rights reserved.
//

#import "WaterFlowLayout.h"
#import "Shops.h"


@interface WaterFlowLayout ()
/************ 属性数组 ************/
@property (nonatomic, strong) NSArray *layoutAttributes;


@end

@implementation WaterFlowLayout
- (void)prepareLayout
{
    // 1.item的宽度，根据列数  内容宽度
    CGFloat contentWidth = self.collectionView.bounds.size.width - self.sectionInset.right - self.sectionInset.left;
    CGFloat itemWidth = (contentWidth - (self.columnCount - 1) *self.minimumInteritemSpacing ) / self.columnCount;
//    self.itemSize = CGSizeMake(itemWidth, 120);

    // 2.计算布局属性
    [self attributes:itemWidth];
}
/************ 计算布局属性 ************/
- (void)attributes:(CGFloat)itemWidth
{
    // 定义一个列高数组,记录每一列最大高度
    CGFloat colHeight[self.columnCount];
    // 每列中item的计数
    NSInteger colCount[self.columnCount];
    for (int i = 0; i < self.columnCount; i++) {
        colHeight[i] = self.sectionInset.top;
        colCount[i] = 0;
    }
    // 定义总item的高
    CGFloat totalItemHeight = 0;
    
    
    NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:self.dataList.count];
    
    NSInteger index = 0;
    // 遍历数组，计算相关属性
    for (Shops *shop in self.dataList) {
        // 1.建立布局属性
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
        UICollectionViewLayoutAttributes *attr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        
        // 2.计算当前列数
        NSInteger col = index % self.columnCount;
//        NSInteger col = [self shortestCol:colHeight];
        // 将对应列数的数组计数+1
        colCount[col]++;
        
        // 3.设置frame
        CGFloat x = self.sectionInset.left + col *(itemWidth + self.minimumInteritemSpacing);
        CGFloat y = colHeight[col];
        
        CGFloat h = [self itemHeightWith:CGSizeMake(shop.w, shop.h) itemWidth:itemWidth];
        totalItemHeight += h;
        attr.frame = CGRectMake(x, y, itemWidth, h);
        
        // 4.累加列高
        colHeight[col] += h + self.minimumInteritemSpacing;
        index++;
        
        [arrM addObject:attr];
    }
    
    // 找到最高的列
    NSInteger highestCol = [self hightestCol:colHeight];
    CGFloat h = (colHeight[highestCol] - colCount[highestCol] * self.minimumLineSpacing) / colCount[highestCol];
    // collectionView的contentSize是由itemSize来计算获得的
    self.itemSize = CGSizeMake(itemWidth ,h);
    
    
    // 添加页脚属性
    UICollectionViewLayoutAttributes *footAttr = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter withIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
    footAttr.frame = CGRectMake(0, colHeight[highestCol]-self.minimumLineSpacing, self.collectionView.bounds.size.width,50);
    [arrM addObject:footAttr];
    
    
    
    // 给属性数组赋值
    self.layoutAttributes = arrM.copy;
}

/****************** 计算最高的列 ******************/
- (NSInteger)hightestCol:(CGFloat *)colHeight
{
    CGFloat max = 0;
    NSInteger col = 0;
    
    for (int i =0; i < self.columnCount; i++) {
        if (colHeight[i] > max) {
            max = colHeight[i];
            col = i;
        }
    }
    return col;
}

/************* 计算最短的列 *************/
- (NSInteger)shortestCol:(CGFloat *)colHeight
{
    CGFloat min = MAXFLOAT;
    NSInteger col = 0;
    
    for (int i =0; i < self.columnCount; i++) {
        if (colHeight[i] < min) {
            col = i;
        }
    }
    return col;
}

/********** 等比例计算item的高度 **********/
- (CGFloat)itemHeightWith:(CGSize)size itemWidth:(CGFloat)itemWidth
{
    return size.height * itemWidth /size.width;
}



- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    return self.layoutAttributes;
}


/************ 返回item的属性数组 ************/
// 计算完成后所有属性会被缓存，不会再次计算
//- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
//{
//    NSArray *array = [super layoutAttributesForElementsInRect:rect];
//    NSMutableArray *arrM = [NSMutableArray arrayWithArray:array];
//    [arrM enumerateObjectsUsingBlock:^(UICollectionViewLayoutAttributes *attr, NSUInteger idx, BOOL *stop) {
//        attr.frame = CGRectMake(0, 0, 200, 200);
//    }];
//    
//    return array;
//}

@end
